package com.springx.examples.showcase.service;

import com.springx.examples.showcase.entity.AdminRole;
import com.springx.examples.showcase.repository.mybatis.AdminRoleMapper;
import com.springx.starter.utils.ConvertUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by roman_000 on 2015/9/12.
 */
@Service
@Transactional
public class AdminRoleService extends BaseService<AdminRole> {
    @Autowired
    private AdminRoleMapper adminRoleMapper;

    /**
     * 批量插入用户角色信息
     */
    public void insert(Map<String, String> roleData, Long adminId) {
        if (MapUtils.isNotEmpty(roleData) && adminId != null) {
            AdminRole adminRole = null;
            Boolean check;
            for (String roleId : roleData.keySet()) {
                adminRole = new AdminRole();
                adminRole.setAdminId(adminId);
                adminRole.setRoleId(ConvertUtils.convert2Long(roleId));
                adminRoleMapper.delete(adminRole);
                check = ConvertUtils.convert2Boolean(roleData.get(roleId));
                if (null == check ? false : check) {
                    adminRoleMapper.insertSelective(adminRole);
                }

            }
        }
    }
}
